package com.kedacom.ctsp.authority.controller;

import com.kedacom.ctsp.authority.constant.AccessLoggerConstant;
import com.kedacom.ctsp.authority.constant.AuthorizeConstant;
import com.kedacom.ctsp.authority.entity.Role;
import com.kedacom.ctsp.authority.service.RoleService;
import com.kedacom.ctsp.authority.vo.RoleVO;
import com.kedacom.ctsp.authz.Authorize;
import com.kedacom.ctsp.logging.AccessLogger;
import com.kedacom.ctsp.web.controller.AbstractCrudController2;
import com.kedacom.ctsp.web.controller.message.ResponseMessage;
import com.kedacom.ctsp.web.controller.util.QueryParamUtil;
import com.kedacom.ctsp.web.entity.page.PagerResult;
import com.kedacom.ctsp.web.entity.param.QueryParamEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

import static com.kedacom.ctsp.web.controller.message.ResponseMessage.ok;


/**
 * 角色控制器
 *
 * @author
 */
@RequestMapping("role")
@ResponseBody
@Authorize(resources = AuthorizeConstant.AUTH_ROLE)
@AccessLogger(AccessLoggerConstant.Module.ROLE)
@Api(tags = "role-manager", description = "角色管理")
public class AuthorityRoleController extends AbstractCrudController2<Role, String, RoleVO, RoleService> {
    /**
     * 获取角色列表信息
     *
     * @param param
     * @param request
     * @return
     */
    @Authorize(resources = AuthorizeConstant.AUTH_ROLE_QUERY)
    @Override
    public ResponseMessage<PagerResult<RoleVO>> list(QueryParamEntity param, HttpServletRequest request) {
        QueryParamUtil.paddingTerms(param, request);
        PagerResult<Role> pagerResult = this.service.selectPager(param);
        return ResponseMessage.ok(pagerResult.convertTo(this::entityToModel));
    }


    @Authorize(resources = AuthorizeConstant.AUTH_ROLE_UPDATE)
    @Override
    public ResponseMessage<String> updateByPrimaryKey(@PathVariable String id, @RequestBody RoleVO data) {
        return super.patchByPrimaryKey(id, data);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_ROLE_ADD)
    @Override
    public ResponseMessage<String> add(@RequestBody RoleVO data) {
        return super.add(data);
    }

    @GetMapping("/checkSignUnique/{name}")
    @ApiOperation("检测唯一性")
    public ResponseMessage<Boolean> checkChildUnique(@PathVariable String name, @RequestParam String clientSign, @RequestParam String value) {
        return ok(getService().checkChildUnique(name, clientSign, value));
    }
}


